package com.suep.business.service.impl;

import com.suep.business.domain.Order;
import com.suep.business.domain.OrderItem;
import com.suep.business.domain.Store;
import com.suep.business.dto.OrderDTO;
import com.suep.business.enums.OrderStatus;
import com.suep.business.mapper.CouponMapper;
import com.suep.business.mapper.OrderItemMapper;
import com.suep.business.mapper.OrderMapper;
import com.suep.business.mapper.StoreMapper;
import com.suep.business.service.IOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@Service
public class OrderServiceImpl implements IOrderService {

    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private OrderItemMapper orderItemMapper;
    @Autowired
    private CouponMapper couponMapper;
    @Autowired
    private StoreMapper storeMapper;

    /**
     * 下单关键逻辑
     */
    @Override
    @Transactional(rollbackFor = Exception.class)  // 添加事务注解，指定所有异常都回滚
    public void pay(Order order, List<OrderItem> orderItemList,Integer userId){
        order.setUserId(userId);
        // 已经支付状态
        order.setStatus(OrderStatus.PAID);
        //支付完成时间
        order.setPaymentTime(LocalDateTime.now());
        insertOrder(order);

        for(OrderItem orderItem:orderItemList){
            orderItemMapper.insertOrderItem(orderItem);
        }

        //如果有优惠券，把这张优惠券状态改为已经使用
        if(order.getCouponId()!=null){
            couponMapper.useMyCoupon(userId,LocalDateTime.now());
        }
    }
    @Override
    public List<Order> selectOrderInProgress(Integer userId){
        return orderMapper.selectOrderListInProgress(userId);
    }
    @Override
    public List<OrderDTO> selectOrderDTOListByUserId(Integer userId){
        List<OrderDTO> orderDTOList=new ArrayList<>();
        List<OrderItem> orderItemList=new ArrayList<>();
        List<Order> orderList=orderMapper.selectOrderByUserId(userId);
        Long orderId;
        for(Order order: orderList){
            orderId=order.getOrderId();
            orderItemList.clear();
            orderItemList=orderItemMapper.selectOrderItemByOrderId(orderId);
            orderDTOList.add(new OrderDTO(order,orderItemList));
        }
        return orderDTOList;
    }
    @Override
    public void updateNextStatus(Long orderId){
        Order order=orderMapper.selectOrderById(orderId);

        OrderStatus orderStatus=order.getStatus();
        switch (orderStatus){
            case PAID:
//                转换成商家已经接单状态
                orderStatus=OrderStatus.STORE_RECEIVED;
                break;
            case STORE_RECEIVED:
//                切换商家已经完成状态
                orderStatus=OrderStatus.STORE_COMPLETED;
                break;
            case DELIVERYMAN_RECEIVED:
//                切换商品送达状态
                orderStatus=OrderStatus.DELIVERYMAN_COMPLETED;
                break;
            case DELIVERYMAN_COMPLETED:
//                切换用户确认收获状态
                orderStatus= OrderStatus.USER_RECEIVED;
                break;
            default:
                System.out.println("订单状态切换异常");
        }
        orderMapper.updateOrderNextStatus(orderStatus,orderId);
    }



    @Override
    public Integer insertOrder(Order order) {
        return orderMapper.insertOrder(order);
    }

    @Override
    public Order selectOrderById(Long orderId) {
        return orderMapper.selectOrderById(orderId);
    }

    @Override
    public List<OrderDTO> selectOrdersByStoreIdByToday(Integer userId) {
        Store store=storeMapper.getStoreByUserId(userId);
        List<Order> orderList=orderMapper.selectOrderListByStoreIdByToday(store.getStoreId());
        List<OrderDTO> orderDTOList=new ArrayList<>();
        List<OrderItem> orderItemList=new ArrayList<>();
        for(Order order:orderList){
            orderItemList.clear();
            orderItemList.addAll(orderItemMapper.selectOrderItemByOrderId(order.getOrderId()));
            orderDTOList.add(new OrderDTO(order,orderItemList));
        }
        return orderDTOList;
    }

    @Override
    public List<Order> selectOrdersByUserId(Integer userId) {
        return orderMapper.selectOrderByUserId(userId);
    }

    @Override
    public List<Order> selectOrdersByStoreId(Integer userId) {
        Store store=storeMapper.getStoreByUserId(userId);
        return orderMapper.selectOrderByStoreId(store.getStoreId());
    }

    @Override
    public List<Order> selectOrdersByDeliveryManId(Integer deliveryManId) {
        return Collections.emptyList();
    }


    @Override
    public Integer updateOrder(Order order) {
        return orderMapper.updateOrderById(order);
    }

    @Override
    public Integer deleteOrderById(Integer orderId) {
        return orderMapper.deleteOrderById(orderId);
    }
}
